package io;


import java.util.List;

import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.domain.TimeInForce;
import com.binance.api.client.domain.account.NewOrderResponse;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.market.TickerPrice;
import com.binance.api.client.exception.BinanceApiException;
import static com.binance.api.client.domain.account.NewOrder.limitBuy;
import static com.binance.api.client.domain.account.NewOrder.limitSell;


public class Demo {
		public static void main(String[] args) {
			//因为是国外的接口 所以需要翻墙 但是本地翻墙之后需要加上这一串 代码才能正常的访问  否则会链接超时
			String proxyHost = "127.0.0.1";
	        String proxyPort = "1080";
	        System.setProperty("http.proxyHost", proxyHost);
	        System.setProperty("http.proxyPort", proxyPort);
	        System.setProperty("https.proxyHost", proxyHost);
	        System.setProperty("https.proxyPort", proxyPort);
	        
			BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YXPeoSXzpkHpb5pUUoN81DPrqBr3jmSTv76DJTv53yKZI6agAkjHYHKlV9fitEe4", "ptPCSYuYbiCNX1FOJNVn0mujRQoKhBuVMvPQH6K7c6f61zZTQokpRatmZKbRpTf0");
			
			BinanceApiRestClient client = factory.newRestClient();
		
//			TickerStatistics tickerStatistics = client.get24HrPriceStatistics("WINGSBTC");
//			System.out.println(tickerStatistics.getLastPrice());
//		    List<Order> allOrders = client.getAllOrders(new AllOrdersRequest("WINGSBTC").limit(10));
			/**
			 * 	查询所有订单的话  订单有三种状态
			 * 	Canceled 撤销
			 * 	FILLED 完成的订单
			 *	NEW 新订单
			 */
//		    System.out.println(allOrders);
//		    NewOrderResponse newOrderResponse = client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"));
//		    System.out.println(newOrderResponse);
//			long serverTime = client.getServerTime();
//			client.ping();
//			NewOrder order = new NewOrder("LINKETH", OrderSide.BUY, OrderType.LIMIT, TimeInForce.GTC, "1");
//			NewOrderResponse newOrderResponse = client.newOrder(order);
//			Long orderId = newOrderResponse.getOrderId();
//			List<TickerPrice> allPrices = client.getAllPrices();
//			NewOrderResponse newOrderResponse = client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"));
//			System.out.println(allPrices);
//			System.out.println(serverTime);
			/**
			 * 获取未结算订单
			 */
			 List<Order> openOrders = client.getOpenOrders(new OrderRequest("LENDBTC"));
			 
			 System.out.println(openOrders.size());
			 /**
			  * 下一个限价订单 买入 (limitBuy参数1,symbol(商品名字,通过接口获取所有商品 然后放在列表让选择 存好选择好的商品名字) 2:生效时间，以指示订单在执行或过期之前将保持多长时间 直接填TimeInForce.GTC,3:quantity 数量   4:price 单价  数量和单价根据设置的来放进去  )
			  * 买入调用limitBuy
			  * 卖出调用limitSell
			  * NewOrderResponse limitBuy = client.newOrder(limitBuy("LENDBTC", TimeInForce.GTC, "100000", "0.00000001"));
			  *	NewOrderResponse limitSell = client.newOrder(limitSell("LENDBTC", TimeInForce.GTC, "100000", "0.00000001"));
			  * 下单如果Account has insufficient balance for requested action 无论是买入还是卖出 都是余额不租
			  * 如果要撤销订单 就先获取所有未结算订单 循环
			  * for (Order order : openOrders) {
			  *		Long orderId = order.getOrderId();
			  *     //在这里调用撤销的方法 传入订单 参数为2个 第一个是币种名称  第二个是订单id
			  *     try {
			  *			client.cancelOrder(new CancelOrderRequest("WINGSBTC",7122198L));
			  *		} catch (BinanceApiException e) {
			  *			System.out.println(e.getError().getMsg());
			  *		}
			  *	}
			  * 
			  */
			 //这个接口是获取所有的最新价钱和代币名字
			 List<TickerPrice> allPrices = client.getAllPrices();
			 for (TickerPrice tickerPrice : allPrices) {
				System.out.println(tickerPrice.getPrice() + "" +tickerPrice.getSymbol());
			 }
	}
}
